#ifndef MYTREEWIDGET_H
#define MYTREEWIDGET_H

#include <QObject>
#include <QTreeWidget>
#include <QHeaderView>
#include <QLineEdit>
#include <QDebug>
#include <QComboBox>
#include <QAction>
#include <QMenu>
#include <QApplication>
#include <QLabel>
#include <QFileInfo>
#include <QFileInfoList>
#include <QDir>
#include <QDateTime>
#include <QCryptographicHash>
#include <QCompleter>
#include "common.h"
#include "format.h"
#include "basewidgets/mycombobox.h"

/*treewidget数据结构*/
struct VideoStreamItemChild
{
    QTreeWidgetItem *video_stream_item_title;
    QTreeWidgetItem *stream_destination_item;
    QTreeWidgetItem *live_item_root;
    QTreeWidgetItem *local_item;
};

struct MaterialItemChild
{
    QTreeWidgetItem *material_item_title;
    QTreeWidgetItem *duration_item;
    QTreeWidgetItem *content_item;
};

struct DataItemChild
{
    QTreeWidgetItem *material_item_root;
    QMap<int, MaterialItemChild> materials_item_map;

    QTreeWidgetItem *stream_index_item_root;
    QTreeWidgetItem *all_item;
};

struct CabPosItemChild
{
    QTreeWidgetItem *cab_pos_item_item_title;

    //base
    QTreeWidgetItem *id_item;
    QTreeWidgetItem *type_item;
    QTreeWidgetItem *x_item;
    QTreeWidgetItem *y_item;
    QTreeWidgetItem *width_item;
    QTreeWidgetItem *height_item;

    //text
    QTreeWidgetItem *font_item;
    QTreeWidgetItem *size_item;
    QTreeWidgetItem *bold_item;
    QTreeWidgetItem *italic_item;
    QTreeWidgetItem *fore_color_item;
    QTreeWidgetItem *back_color_item;
    QTreeWidgetItem *background_item;
    QTreeWidgetItem *background_color_item;
    QTreeWidgetItem *align_item;
    QTreeWidgetItem *effect_item;
    QTreeWidgetItem *speed_item;

    //video
    QTreeWidgetItem *sound_item;

    //base
    QTreeWidgetItem *data_item;
    DataItemChild data_item_child;
};

struct ItemItemChild
{
    QTreeWidgetItem *item_item_title;
    QMap<QString, CabPosItemChild> cab_pos_items_item_map; //通过item_id寻找item子控件结构体
};

struct LayoutItemChild
{
    QTreeWidgetItem *layout_item_title; //layout1 layout2 ...
    QTreeWidgetItem *id_item;
    QTreeWidgetItem *duration_item;
    QTreeWidgetItem *item_item_root; //items
    QMap<int, ItemItemChild> items_item_map; //通过item_id寻找item子控件结构体
};

struct CommonItemChild
{
    QTreeWidgetItem *x_item;
    QTreeWidgetItem *y_item;
    QTreeWidgetItem *width_item;
    QTreeWidgetItem *height_item;
    QTreeWidgetItem *z_order_item;
    QTreeWidgetItem *transparent_item;
    QTreeWidgetItem *background_item;
    QTreeWidgetItem *background_color_item;
};

struct PartitionItemChild
{
    QTreeWidgetItem *partition_item_title; //partition1 partition2 ...
    QTreeWidgetItem *id_item;
    QTreeWidgetItem *common_item;
    CommonItemChild common_item_child;
    QTreeWidgetItem *layout_item_root; //layouts
    QMap<int, LayoutItemChild> layouts_item_map; //通过layout_id寻找layout控件结构体
};

struct ScreenItemChild
{
    QTreeWidgetItem *screen_item_title; //screen1 screen2 ...
    QTreeWidgetItem *id_item;
    QTreeWidgetItem *name_item;
    QTreeWidgetItem *partition_item_root; //partitions
    QMap<int, PartitionItemChild> partitions_item_map; //方便通过partition_id寻找partition控件结构体
};

class MyTreeWidget : public QTreeWidget
{
    Q_OBJECT
public:
    explicit MyTreeWidget(QWidget *parent = 0);
    ~MyTreeWidget();
    void createTreeWidget(Format *format);
    void createPathPrefixTw(Format *format);
    void createVideoStreamsTw(Format *format);
    void createArchitectureVersionTw(Format *format);
    void createCurrentShowTw(Format *format);
    void createResolutionTw(Format *format);
    void createResourcesTw(Format *format);
    void refreshResourcesTw();
    void createScreensTw(Format *format);

    void addLiveItemChildren(QTreeWidgetItem *parent_item, Live one_live);
    void addVideoStremItemChildren(QTreeWidgetItem *parent_item, VideoStream video_stream);
    void addMaterialItemChildren(DataItemChild &parent_item_struct, QMap<int, Material> materials);
    void addStreamIndexItemChildren(DataItemChild &parent_item_struct, Stream_index stream_index);
    void addCabPosItemItemChildren(ItemItemChild &parent_item_struct, CabPosItem add_cabpositem, QString cabpos = "cab[*]-position[*]");
    void addItemItemChildren(LayoutItemChild &parent_item_struct, Item one_item);
    void addLayoutItemChildren(PartitionItemChild &parent_item_struct, Layout add_layout);
    void addPartitionItemChildren(ScreenItemChild &parent_item_struct, Partition add_partition);
    void addScreenItemChildren(Screen add_screen);

    inline void addPartitionItemChildrenUseId(int screen_id, Partition add_partition)
    {
        addPartitionItemChildren(m_screens_item_map[screen_id], add_partition);
    }
    inline void addLayoutItemChildrenUseId(int screen_id, int partition_id, Layout add_layout)
    {
        addLayoutItemChildren(m_screens_item_map[screen_id].partitions_item_map[partition_id], add_layout);
    }
    inline void addItemItemChildrenUseId(int screen_id, int partition_id, int layout_id, Item add_item)
    {
        addItemItemChildren(m_screens_item_map[screen_id].partitions_item_map[partition_id].layouts_item_map[layout_id], add_item);
    }
    inline void addCabPosItemChildrenUseId(int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str, CabPosItem add_cabpositem)
    {
        addCabPosItemItemChildren(m_screens_item_map[screen_id].partitions_item_map[partition_id].layouts_item_map[layout_id].items_item_map[item_id],
                                  add_cabpositem, cab_pos_item_str);
    }


    //根据item获取id
    int getIdFromItemText(QString item_text);

    //更新数据
    void updateItemItemData(QTreeWidgetItem *parent_item, Item item);
    void updatePartitionItemData(QTreeWidgetItem *parent_item, Partition partition);

    //从每个节点中获取数据
    void getMaterialOrStreamIndexDataFromTw(QTreeWidgetItem *material_or_stream_index_item_root, CabPosItem &one_cab_pos_item);
    void getCabPosItemDataFromTw(QTreeWidgetItem *cab_pos_item_root, Item &one_item);
    void getItemDataFromTw(QTreeWidgetItem *item_item_root, Layout &one_layout);
    void getLayoutDataFromTw(QTreeWidgetItem *layout_item_root, Partition &one_partition);
    void getPartitionDataFromTw(QTreeWidgetItem *partition_item_root, Screen &one_screen);
    void getScreenDataFromTw();
    void getAllDataFromTwView();

    //全部收缩或展开
    void setAllItemsReduce(bool flag);
    //tab触发tree选中
    void setAllItemsLossSelect();
    //选择自动展开
    void autoExpandOrReduceByCurrClickItem(QTreeWidgetItem *curr_item);
    void setAllChildrenHighLight(QTreeWidgetItem *curr_item);
    void setSelectedScreenItem(int screen_id);
    void setSelectedPartitionItem(int screen_id, int partition_id);
    void setSelectedLayoutItem(int screen_id, int partition_id, int layout_id);
    void setSelectedItemItem(int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str);

    inline QString getLineEditItemChildText(QTreeWidgetItem *root_item, int child_index) {
        return static_cast<QLineEdit *>(itemWidget(root_item->child(child_index), 1))->text();
    }
    inline QString getLabelItemChildText(QTreeWidgetItem *root_item, int child_index) {
        return static_cast<QLabel *>(itemWidget(root_item->child(child_index), 1))->text();
    }
    inline bool getComboBoxItemChildBool(QTreeWidgetItem *root_item, int child_index) {
        return static_cast<QComboBox *>(itemWidget(root_item->child(child_index), 1))->currentText() == m_combo_text_tf.first() ? true : false;
    }
    inline int getComboBoxItemChildIndex(QTreeWidgetItem *root_item, int child_index) {
        return static_cast<QComboBox *>(itemWidget(root_item->child(child_index), 1))->currentIndex();
    }
    inline QString getComboBoxItemChildText(QTreeWidgetItem *root_item, int child_index) {
        return static_cast<QComboBox *>(itemWidget(root_item->child(child_index), 1))->currentText();
    }
    inline QString getLineEditItemText(QTreeWidgetItem *root_item) {
        return static_cast<QLineEdit *>(itemWidget(root_item, 1))->text();
    }
    inline QString getLabelItemText(QTreeWidgetItem *root_item) {
        return static_cast<QLabel *>(itemWidget(root_item, 1))->text();
    }

    void removeAllChildren(QTreeWidgetItem *root);
    void removeScreenItem(int screen_id);
    void removeCurrentClickItem();

private slots:
    void itemPressedSlot(QTreeWidgetItem *pressed_item, int column);
    void on_addItem();
    void on_delItem();
    void on_refresh();

signals:
    screen_item_clicked(int);
    partition_item_clicked(int, int);
    layout_item_clicked(int, int, int);
    item_item_clicked(int, int, int, int, QString);

    add_screen();
    del_screen(int);
    add_partition(int);
    del_partition(int, int);
    add_layout(int, int);
    del_layout(int, int, int);
    add_item(int, int, int);
    del_item(int, int, int, int);
    add_cabpos(int, int, int, int);
    del_cabpos(int, int, int, int, QString);
    update_clicked();

private:
    QTreeWidgetItem *m_path_prefix_item_root;
    QTreeWidgetItem *m_video_streams_item_root;
    QTreeWidgetItem *m_architecture_version_item_root;
    QTreeWidgetItem *m_current_show_item_root;
    QTreeWidgetItem *m_resolution_item_root;
    QTreeWidgetItem *m_resources_item_root;
    QTreeWidgetItem *m_screens_item_root;//screens
    QMap<int, ScreenItemChild> m_screens_item_map;//方便通过screen_id寻找screen控件结构体

    QAction *m_action_add_item;
    QAction *m_action_del_item;
    QAction *m_action_refresh_res_item;

    Format *m_format;

    QTreeWidgetItem *m_current_click_item;

    const QStringList m_combo_text_tf = {tr("true"), tr("false")};
    const QStringList m_combo_text_align = {tr("left"), tr("center"), tr("right")};
    const QString     m_edit_qss = "border : none;"
                                   "border-left : 1px solid lightgray;"
                                   "background-color : transparent;"
                                   "qproperty-cursorPosition: 0";
    QStringList m_img_list;
};

#endif // MYTREEWIDGET_H
